<?php
// 视频模块
class VideoAction extends CommonAction {
	//赋值视频可用分类
	public function _before_add() {
		$list	=	M("Category")->where('classstatus=1 AND classmodule="Video"')->select();
		$this->assign('list',$list);
	}
	//赋值视频可用分类
	public function _before_edit() {
		$this->_before_add();
	}
	//添加视频,上传视频图片
	public function insert() {
		$model = D ('Video');
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		if(!empty($_FILES['img']['name'])){
			import("ORG.Net.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize  = 1048576 * 3; //3M
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg'); 
			$upload->savePath =  './Public/Upload/article/';
			$upload->saveRule = 'uniqid';
			$upload->thumb = true;
			$upload->thumbMaxWidth = 100;
			$upload->thumbMaxHeight = 100;
			$upload->uploadReplace = false;
			$upload->thumbPrefix = '100_100_';
			if(!$upload->upload()) { 
				$this->error($upload->getErrorMsg());
			}else{
				$imgs = $upload->getUploadFileInfo(); 
				$model->img = $imgs[0]['savename'];
			}
		}else{
			$model->img = $_POST['imgurl']!='' ? $_POST['imgurl'] : $model->img;
		}
		if($model->img!=''){ //删除旧文件
			$oldfile = $model->where(is_numeric($_POST['id']) ? $_POST['id'] : $model->getLastInsID())->getField('img');
			$oldfile = $_SERVER['DOCUMENT_ROOT'].chimg($oldfile,'video',100,100);
			if(is_file($oldfile)) unlink($oldfile);
			$oldfile = $_SERVER['DOCUMENT_ROOT'].chimg($oldfile,'video');
			if(is_file($oldfile)) unlink($oldfile);
		}
		if(!empty($_FILES['file']['name'])){
			import("ORG.Net.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize  = 1048576*1024 ; //1G
			$upload->allowExts  = array('swf', 'flv', 'wmv'); 
			$upload->savePath =  './Public/Upload/video/';
			$upload->saveRule = 'uniqid';
			$upload->uploadReplace = true;
			if(!$upload->upload()) { 
				$this->error('视频文件：'.$upload->getErrorMsg());
			}else{
				$files = $upload->getUploadFileInfo(); 
				$model->file = $files[0]['savename'];
			}
		}else{
			$model->file = $_POST['fileurl']!='' ? $_POST['fileurl'] : $model->file;
		}		
		if($model->file!=''){ //删除旧文件
			$oldfile = $model->where(is_numeric($_POST['id']) ? $_POST['id'] : $model->getLastInsID())->getField('file');
			$oldfile = $_SERVER['DOCUMENT_ROOT'].chimg($oldfile,'video');
			if(is_file($oldfile)) unlink($oldfile);
		}
		//保存当前数据对象
		if ($model->add ()!==false) { //保存成功
			if($_POST['rewrite']){
				$data['rewrite']=$_POST['rewrite'];
				$data['url']='video/view/id/'.$model->getLastInsID();
				if(!D('Router')->add($data)){
					$model->where( 'id='.(is_numeric($_POST['id']) ? $_POST['id'] : $model->getLastInsID()) )->setField('rewrite','');
				}
			}
			echo '<script type="text/javascript">
					var response = {
						"status":"1",
						"info":"\u64cd\u4f5c\u6210\u529f",
						"navTabId":"Video",
						"forwardUrl":"",
						"callbackType":"closeCurrent"
					};
					if(window.parent.donecallback) {
						 window.parent.donecallback(response);
					}
			    </script>';
		} else {
			//失败提示
			$this->error ('新增失败!');
		}		
	}
	//更新视频
	public function update(){
		$model = D ( 'Video' );
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}		
		if(!empty($_FILES['img']['name'])){
			import("ORG.Net.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize  = 1048576 * 3; //3M
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg'); 
			$upload->savePath =  './Public/Upload/article/';
			$upload->saveRule = 'uniqid';
			$upload->thumb = true;
			$upload->thumbMaxWidth = 100;
			$upload->thumbMaxHeight = 100;
			$upload->uploadReplace = false;
			$upload->thumbPrefix = '100_100_';
			if(!$upload->upload()) { 
				$this->error($upload->getErrorMsg());
			}else{
				$imgs = $upload->getUploadFileInfo(); 
				$model->img = $imgs[0]['savename'];
			}
		}else{
			$model->img = $_POST['imgurl']!='' ? $_POST['imgurl'] : $model->img;
		}
		if($model->img!=''){ //删除旧文件
			$oldfile = $model->where(is_numeric($_POST['id']) ? $_POST['id'] : $model->getLastInsID())->getField('img');
			$oldfile = $_SERVER['DOCUMENT_ROOT'].chimg($oldfile,'video',100,100);
			if(is_file($oldfile)) unlink($oldfile);
			$oldfile = $_SERVER['DOCUMENT_ROOT'].chimg($oldfile,'video');
			if(is_file($oldfile)) unlink($oldfile);
		}
		if(!empty($_FILES['file']['name'])){
			import("ORG.Net.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize  = 1048576*1024 ; //1G
			$upload->allowExts  = array('swf', 'flv', 'wmv'); 
			$upload->savePath =  './Public/Upload/video/';
			$upload->saveRule = 'uniqid';
			$upload->uploadReplace = true;
			if(!$upload->upload()) { 
				$this->error('视频文件：'.$upload->getErrorMsg());
			}else{
				$files = $upload->getUploadFileInfo(); 
				$model->file = $files[0]['savename'];
			}
		}else{
			$model->file = $_POST['fileurl']!='' ? $_POST['fileurl'] : $model->file;
		}		
		if($model->file!=''){ //删除旧文件
			$oldfile = $model->where(is_numeric($_POST['id']) ? $_POST['id'] : $model->getLastInsID())->getField('file');
			$oldfile = $_SERVER['DOCUMENT_ROOT'].chimg($oldfile,'video');
			if(is_file($oldfile)) unlink($oldfile);
		}
		//保存当前数据对象
		if ($model->save () !== false) {
			//成功提示
			D('Router')->where("url='video/view/id/{$_POST['id']}'")->delete();
			if($_POST['rewrite']){
				$data['url']="video/view/id/".$_POST['id'];
				$data['rewrite']=$_POST['rewrite'];
				if(!D('Router')->add($data)){
					$model->where( 'id='.(is_numeric($_POST['id']) ? $_POST['id'] : $model->getLastInsID()) )->setField('rewrite','');
				}
			}
			$this->success ('编辑成功!');
		} else {
			//错误提示
			$this->error ('编辑失败!');
		}	
	}
	
	//删除测试时删除预览图片,删除路由规则
	public function _before_foreverdelete() {
		$ids = $this->_beforDelFiles(MODULE_NAME);
		if($ids){
			$list = M(MODULE_NAME)->where( array(M(MODULE_NAME)->getPk()=>array('in', $ids)) )->field('rewrite')->select();
			foreach($list as $rs) $rewrite .= ($rewrite ? "','" : '') . $rs['rewrite'];
			if($rewrite) M('Router')->where( "rewrite in ('{$rewrite}')" )->delete();
		}
	}
	
	public function view(){
		is_numeric($_GET['id']) or $this->error('参数错误！');
		$this->id = $_GET['id'];
		$this->display();
	}
}